package prictice.userVisit;

import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.HashMap;
import java.util.Map;

/**
 * @Package prictice.userVisit
 * @Description:
 * @Author elwyn
 * @Date 2017/9/16 17:04
 * @Email elonyong@163.com
 */
public class DeepVisitBolt implements IBasicBolt {
    @Override
    public void prepare(Map map, TopologyContext topologyContext) {

    }

    Map<String, Integer> counts = new HashMap<>();

    @Override
    public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
        String dateString = tuple.getString(0);
        String sessionId = tuple.getString(1);
        Integer count = counts.get(dateString + "_" + sessionId);
        if (count == null) {
            count = 0;
        }
        count++;
        counts.put(dateString + "_" + sessionId, count);
        basicOutputCollector.emit(new Values(dateString + "_" + sessionId, count));
    }

    @Override
    public void cleanup() {

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("date_session", "count"));
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
